﻿using UnityEngine;
using System.Collections.Generic;
using CinemaDirector.Helpers;

namespace CinemaDirector
{
    /// <summary>
    /// A custom event that will set a GameObject's specific component enable state at a given time. 
    /// </summary>
    [CutsceneItemAttribute("Cool", "SetAnimatorPlaySpeed", CutsceneItemGenre.ActorItem)]
    public class SetAnimatorPlaySpeed : CinemaActorAction
    {

        private Animator actorAnimator = null;
        private bool actorAnimatorPreEnabled = false;

        [SerializeField]
        private float playSpeed = 1;
        private float animatorRunningTime = 0;

        public float PlaySpeed
        {
            get { return playSpeed; }
            set { playSpeed = value; }
        }
        public override void Trigger(GameObject actor)
        {
            actorAnimator = actor.GetComponent<Animator>();
            actorAnimatorPreEnabled = actorAnimator.enabled;
            actorAnimator.enabled = false;
        }

        /// <summary>
        /// Continue to update the transform to look at the target.
        /// </summary>
        /// <param name="actor">The actor being updated.</param>
        /// <param name="runningTime">The running time of the cutscene.</param>
        /// <param name="deltaTime">The deltaTime since last call.</param>
        public override void UpdateTime(GameObject actor, float runningTime, float deltaTime)
        {
            if (actor == null || actorAnimator == null) return;
            var animatorDeltaTime = deltaTime * playSpeed;
            actorAnimator.Update(animatorDeltaTime);
        }

        public override void End(GameObject Actor)
        {
            if (actorAnimator == null)
                return;

            animatorRunningTime = 0;
            actorAnimator.enabled = actorAnimatorPreEnabled;
        }

        public override void Stop(GameObject Actor)
        {
            End(Actor);
        }

       
    }
}